package cn.itsuorce.controller;

import cn.itsuorce.domain.Employee;
import cn.itsuorce.domain.Systemdictionarydetail;
import cn.itsuorce.query.EmployeeQuery;
import cn.itsuorce.service.IEmployeeService;
import cn.itsuorce.utils.AjaxResult;
import cn.itsuorce.utils.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 根据id查询部门经理
     * @param job_id
     * @return
     */
    @GetMapping("/employee/findByJob_id/{job_id}")
    private List<Employee> findByJob_id (@PathVariable("job_id")Long job_id){
        return employeeService.findByJob_id(job_id);
    }
    @GetMapping("/employee/managerjobs/{job_id}")
    private List<Systemdictionarydetail> managerjobs (@PathVariable("job_id")Long job_id){
        return employeeService.managerjobs(job_id);
    }

    /**
     * 新增或修改
     * @param employee
     * @return
     */
    @PutMapping("/employee")
    @ApiOperation(value = "新增或修改员工信息", tags = "新增或修改员工信息")
    public AjaxResult save(@RequestBody Employee employee) {
        try {
            System.out.println(employee);
            employeeService.save(employee);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！" + e.getMessage());
        }
    }
    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/employee/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/employee/batch")
    public AjaxResult delete(@RequestBody List<Long> ids) {
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！" + e.getMessage());
        }
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/employee/{id}")
    public Employee findOne(@PathVariable("id") Long id) {
        return employeeService.findOne(id);
    }

    /**
     * 查询所有，支持高级查询的条件
     *
     * @param employeeQuery
     * @return
     */
    @PostMapping("/employee")
    public List<Employee> findAll(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.findAll(employeeQuery);
    }

    /**
     * 分页查询
     *
     * @param employeeQuery
     * @return
     * @RequestBody 表示从请求消息体中接收前端发送过来的json字符串，并且使用jackson工具包自动转化为所需要的实体类类型的对象
     */
    @PatchMapping("/employee")
    public AjaxResult page(@RequestBody EmployeeQuery employeeQuery) {
        try {
            System.out.println(employeeQuery);
            Page<Employee> page = employeeService.findPage(employeeQuery);
            return AjaxResult.me().setData(page);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！" + e.getMessage());
        }
    }
}
